package com.djl.rabbitmq.five;

import com.djl.rabbitmq.utils.RabbitMqUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;

/**
 * @program: rabbitmq-hello
 * @description: 消息的接收
 * @author: Mr.dai
 * @create: 2021-09-10 17:54
 **/

public class ReceiveLogs01 {
    public static final String EXCHANGE_NAME="logs";
    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
    //    声明一个交换机
        channel.exchangeDeclare(EXCHANGE_NAME,"fanout");
        /**声明一个临时队列
         //    队列的名称时随机的，
         //    消费者断开连接
         *
         */
        /**
         * 生成临时队列
         * 队列名称随机
         * 当消费者断开连接时 队列就自动删除了
         */
        String queueName = channel.queueDeclare().getQueue();
        /**
         * 绑定交换机
         */
        channel.queueBind(queueName,EXCHANGE_NAME,"");
        System.out.println("等待接收消息，把接收到的消息打印到屏幕上。。。。");
        //
        DeliverCallback deliverCallback=( consumerTag,  message)->{
            System.out.println("ReceiveLogs01接收控制台打印接收到的消息："+new String(message.getBody(),"UTF-8"));
        };
        channel.basicConsume(queueName,true,deliverCallback,consumerTag->{});
    }
}
